package com.sugar.follow.up.module.business.dal.mysql.userpatient;

import com.sugar.follow.up.framework.mybatis.core.mapper.BaseMapperX;
import com.sugar.follow.up.framework.mybatis.core.query.MPJLambdaWrapperX;
import com.sugar.follow.up.framework.mybatis.core.type.StringListTypeHandler;
import com.sugar.follow.up.module.business.controller.admin.userpatient.vo.UserPatientRespVO;
import com.sugar.follow.up.module.business.controller.app.userpatient.vo.AppUserPatientSimpleRespVO;
import com.sugar.follow.up.module.business.dal.dataobject.userpatient.UserPatientDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 就诊人 Mapper
 *
 * @author Ysugar
 */
@Mapper
public interface UserPatientMapper extends BaseMapperX<UserPatientDO> {


    @Select("<script>" +
            "select" +
            " count(1) " +
            "from " +
            "<if test=\"param1 != null\"> (${param1}) tag_b left join business_user_patient bup on (tag_b.patient_id = bup.id and bup.deleted = 0) </if>" +
            "<if test=\"param1 == null\"> business_user_patient bup </if>" +
            "left join system_users su on (su.id = bup.doctor_id AND su.deleted = 0) " +
            "where bup.deleted = 0 " +
            "<if test=\"param2 != null and param2 != ''\"> bup.name = #{param2} </if>" +
            "<if test=\"param3 != null and param3 != ''\"> bup.mobile = #{param3} </if>" +
            "<if test=\"param4 != null and param4 != ''\"> bup.id_card = #{param4} </if>" +
            "<if test=\"param5 != null and param5 != ''\"> bup.id_code = #{param5} </if>" +
            "<if test=\"param6 != null\"> bup.preferred_plan = #{param6} </if>" +
            "<if test=\"param7 != null\"> bup.status = #{param7} </if>" +
            "<if test=\"param8 != null and param8 != ''\"> bup.risk_level = #{param8} </if>" +
            "<if test=\"param9 != null and param9 != ''\"> su.nickname = #{param9} </if>" +
            "<if test=\"param10 != null\"> and bup.doctor_id = #{param10} </if>" +
            "order by bup.id desc " +
            "</script>")
    Long customSelectListCount(String tempTab, String name, String mobile, String idCard, String idCode,
                                                     Integer preferredPlan, Boolean status, String riskLevel, String doctorName, Long doctorId);


    @Select("<script>" +
            "select" +
            " bup.id, bup.name, bup.mobile, bup.id_card, bup.id_code, bup.id_card, bup.status, bup.doctor_id," +
            " bup.risk_level, bup.location, bup.location_detail, bup.birthday, bup.confirmed_diagnosis_age, bup.special_illness," +
            " su.nickname as doctor_name " +
            "from " +
            "<if test=\"param1 != null\"> (${param1}) tag_b left join business_user_patient bup on (tag_b.patient_id = bup.id and bup.deleted = 0) </if>" +
            "<if test=\"param1 == null\"> business_user_patient bup </if>" +
            "left join system_users su on (su.id = bup.doctor_id AND su.deleted = 0) " +
            "where bup.deleted = 0 " +
            "<if test=\"param2 != null and param2 != ''\"> bup.name = #{param2} </if>" +
            "<if test=\"param3 != null and param3 != ''\"> bup.mobile = #{param3} </if>" +
            "<if test=\"param4 != null and param4 != ''\"> bup.id_card = #{param4} </if>" +
            "<if test=\"param5 != null and param5 != ''\"> bup.id_code = #{param5} </if>" +
            "<if test=\"param6 != null\"> bup.preferred_plan = #{param6} </if>" +
            "<if test=\"param7 != null\"> bup.status = #{param7} </if>" +
            "<if test=\"param8 != null and param8 != ''\"> bup.risk_level = #{param8} </if>" +
            "<if test=\"param9 != null and param9 != ''\"> su.nickname = #{param9} </if>" +
            "<if test=\"param10 != null\"> and bup.doctor_id = #{param10} </if>" +
            "order by bup.id desc " +
            "limit #{param11}, #{param12}" +
            "</script>")
    @Results(@Result(column = "location", property = "location", typeHandler = StringListTypeHandler.class))
    List<UserPatientRespVO> customSelectList(String tempTab, String name, String mobile, String idCard, String idCode,
                                                     Integer preferredPlan, Boolean status, String riskLevel, String doctorName, Long doctorId,
                                                     int skipCount, Integer pageSize);

    default List<AppUserPatientSimpleRespVO> getUserPatientAllList(Long userId){
        return selectJoinList(AppUserPatientSimpleRespVO.class, new MPJLambdaWrapperX<UserPatientDO>()
                .select(UserPatientDO::getId, UserPatientDO::getName, UserPatientDO::getMobile, UserPatientDO::getIdCard, UserPatientDO::getRiskLevel)
                .eq(UserPatientDO::getUserId, userId)
                .orderByDesc(UserPatientDO::getId));
    }

    @Select("select temp_tbl.patient_id from (${param1}) temp_tbl")
    List<Long> execSql(String sql);
}